Arr.update   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
crap 1
1
import multisort from './modules/multisort.js';
2
import multifilter from './modules/multifilter.js';
3
import multikey from './modules/multikey.js';
4
import intersect from './modules/intersect.js';
5
import min from './modules/min.js';
6
import max from './modules/max.js';
7
import diff from './modules/diff.js';
8
import unique from './modules/unique.js';
9
import summ from './modules/summ.js';
10
import average from './modules/average.js';
11
import random from './modules/random.js';
12
import getByKey from './modules/getByKey.js';
13
import first from './modules/first.js';
14
import last from './modules/last.js';
15
import update from './modules/update.js';
16
17
/**
18
 * Object helper
19
 */
20
class Arr extends Array {
21
    static get [Symbol.species]() {
22 1
        return Array;
23
    }
24
25
    /**
26
     * Sort a multiarray.
27
     *
28
     * @param {string} key
29
     * @param {string} direction
30
     *
31
     * @return {array}
32
     */
33
    multisort(key, direction) {
34 4
        return multisort(this[0], key, direction);
35
    }
36
37
    /**
38
     * Filter a multi array.
39
     *
40
     * @param {string}  key
41
     * @param {string}  find
42
     * @param {boolean} operator
43
     *
44
     * @return {array}
45
     */
46
    multifilter(key, find, operator) {
47 14
        return multifilter(this[0], key, find, operator);
48
    }
49
50
    /**
51
     * Only get some keys of a multi array.
52
     *
53
     * @param {string} key
54
     *
55
     * @return {array}
56
     */
57
    multikey(key) {
58 2
        return multikey(this[0], key);
59
    }
60
61
    /**
62
     * Get the intersection of arrays.
63
     *
64
     * @param {any[]}  array
65
     * @param {boolean} multi
66
     *
67
     * @return {array}
68
     */
69
    intersect(array, multi) {
70 2
        return intersect(this[0], array, multi);
71
    }
72
73
    /**
74
     * Get the difference of arrays.
75
     *
76
     * @param {any[]}  array
77
     * @param {boolean} total
78
     *
79
     * @return {array}
80
     */
81
    diff(array, total) {
82 2
        return diff(this[0], array, total);
83
    }
84
85
    /**
86
     * Get the unique values of an array.
87
     *
88
     * @return {any[]}
89
     */
90
    get unique() {
91 1
        return unique(this[0]);
92
    }
93
94
    /**
95
     * Only add the value if the value isnt in the array.
96
     *
97
     * @param {any[]} newValue
98
     *
99
     * @return {number}
100
     */
101
    pushIfNotExists(newValue) {
102 9
        if (this.indexOf(newValue) < 0) {
103 6
            this.push(newValue);
104
        }
105
106 9
        return this.length;
107
    }
108
109
    /**
110
     * Add multiple values to an array.
111
     *
112
     * @param {any[]} newValues
113
     *
114
     * @return {number}
115
     */
116
    pushMultiple(newValues) {
117 2
        this.push(...newValues);
118
119 2
        return this.length;
120
    }
121
122
    /**
123
     * Add multiple values to an array.
124
     * Only add the value if the value isnt in the array.
125
     *
126
     * @param {array} newValues
127
     *
128
     * @return {number}
129
     */
130
    pushMultipleIfNotExists(newValues) {
131 3
        const array = this;
132
133 3
        newValues.forEach((value) => {
134 6
            array.pushIfNotExists(value);
135
        });
136
137 3
        return array.length;
138
    }
139
140
    /**
141
     * The largest of the given numbers.
142
     * If at least one of the arguments cannot be converted to a number,
143
     * NaN is returned.
144
     *
145
     * @return {number}
146
     */
147
    get max() {
148 2
        return max(this[0]);
149
    }
150
151
    /**
152
     * The smallest of the given numbers.
153
     * If at least one of the arguments cannot be converted to a number,
154
     * NaN is returned.
155
     *
156
     * @return {number}
157
     */
158
    get min() {
159 2
        return min(this[0]);
160
    }
161
162
    /**
163
     * Get a random value of an array.
164
     *
165
     * @return {any}
166
     */
167
    get random() {
168 1
        return random(this);
169
    }
170
171
    /**
172
     * The summ of all values.
173
     *
174
     * @return {number}
175
     */
176
    get summ() {
177 2
        return summ(this[0]);
178
    }
179
180
    /**
181
     * Get the average of all values.
182
     *
183
     * @return {number}
184
     */
185
    get average() {
186 2
        return average(this[0]);
187
    }
188
189
    /**
190
     * Javascript implementation of Arr::get
191
     *
192
     * @param {string} key
193
     * @param {object|null} defaultValue
194
     *
195
     * @return {object|null}
196
     */
197
    getByKey(key, defaultValue) {
198 3
        return getByKey(this[0], key, defaultValue);
199
    }
200
201
    /**
202
     * Javascript implementation of Arr::first
203
     *
204
     * @return {object|null}
205
     */
206
    get first() {
207 2
        return first(this[0]);
208
    }
209
210
    /**
211
     * Javascript implementation of Arr::last
212
     *
213
     * @return {object|null}
214
     */
215
    get last() {
216 2
        return last(this[0]);
217
    }
218
219
    /**
220
     * Update multiple items in an array
221
     *
222
     * @param {array} newValues
223
     * @param {array} keys
224
     *
225
     * @return {array}
226
     */
227
    update(newValues, keys) {
228 5
        return update(this[0], newValues, keys);
229
    }
230
}
231
232
export {
233
    Arr,
234
    multisort,
235
    multifilter,
236
    multikey,
237
    intersect,
238
    min,
239
    max,
240
    diff,
241
    unique,
242
    summ,
243
    average,
244
    random,
245
    getByKey,
246
    first,
247
    last,
248
    update,
249
};
250
251
export default Arr;
252